<?php
/**
* Model Class
* 
* @author Guillermo Landín
* @link 
*/ 

class Friendsmodel extends Model {

    function __construct()
    {
        parent::Model();
        
        $this->CI =& get_instance(); 
    }
    
    function get_list(){
    
         $this->db->select('id_Friends AS id, Title_Friends AS title, Introduction_Friends As text')
                ->from('friends')
                ->where('Active_Friends', 1)  
                ->order_by('Title_Friends', 'ASC');            

        $query = $this->db->get();

        return $query->result_array();
    
    }

    //returns all the details for single item $id for display in detail page
    public function get_item($id = NULL){

        $this->db->select('*')
                ->from('friends')
                ->where('id_Friends', $id)
                ->where('Active_Friends', 1);       

        $query = $this->db->get();

        return $query->row_array();

    }

 
/***********************************   
*   Sub-menu functions
***********************************/

    function get_submenu_list($id = FALSE){
    
         $this->db->select('id_Friends AS id, Title_Friends AS title')
                ->from('friends')
                ->where('Active_Friends', 1)
                ->order_by('Title_Friends', 'ASC');            

        $query = $this->db->get();

        $result = $query->result_array();     

        foreach($result as &$row){
            $row = array( 
                        'name' => $row['title'], 
                        'controller' => 'friends/view/'.$row['id'], 
                        'param' => $row['id']
                        );
        }

        //set the selected item
        if ($id !== FALSE) {
            foreach($result as &$item){
                if ( $item['param'] == $id) $item['selected'] = TRUE;
            }
        }

        return $result;
    
    }


/***********************************
*   Backoffice functions
***********************************/

    //returns the list of friends
    function admin_get_list($fields='*'){
    
        $this->db->select($fields)
                ->from('friends')
                ->order_by('Title_Friends', 'ASC');  

        $query = $this->db->get();

        $result = $query->result_array();
                                 
        return $result;

    }  
    public function admin_get_item($id=FALSE, $fields='*'){

        $this->db->select($fields)
                ->from('friends')       
                ->where('id_Friends', $id);  
        
        $query = $this->db->get();
        return $query->row_array();

    }  
    public function admin_update($id=FALSE, $data=array())
    {
        $this->db->where('id_Friends', $id)
                ->update('friends', $data); 
    }                      
    public function admin_insert($data=array()){

        $data["id_Friends"] = uniqid();
        $data["Active_Friends"] = 0;
        $this->db->insert('friends', $data); 
        return $data["id_Friends"];

    }  
    public function admin_delete($id=FALSE){

        $this->db->where('id_Friends', $id);
        $this->db->delete('friends'); 

    } 
    public function is_duplicated($field, $value, $id=FALSE)
    {
    
        $this->db->select('count(1) as counter')
                ->from('friends')
                ->where($field, $value);        
        if($id!==FALSE) $this->db->where('id_Friends !=', $id);
    
        $query = $this->db->get();
        $row = $query->row_array();
        if($row == NULL)
            return FALSE;
        
        if($row["counter"] > 0){
            return TRUE;
        }else{
            return FALSE;
        }

    } 


}
    
    
/* End of file newsmodel.php */

/* Location: ./system/application/models/newsmodel.php */ 
